package es.deusto.ingenieria.sd.tralala.server.db;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.jdo.Extent;
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;
import javax.jdo.Transaction;

import es.deusto.ingenieria.sd.tralala.server.data.Cancion;
import es.deusto.ingenieria.sd.tralala.server.data.Reproduccion;
import es.deusto.ingenieria.sd.tralala.server.data.Usuario;
import es.deusto.ingenieria.sd.tralala.server.data.cancionAssembler;
import es.deusto.ingenieria.sd.tralala.server.data.cancionDTO;

public class JDO implements IDAO {
	PersistenceManagerFactory pmf;
	PersistenceManager pm;
	
	private static IDAO instance;

	
	public static IDAO getInstance() {
		if (instance == null) {
			instance = new JDO();
		}

		return instance;
	}


	public JDO() {
		pmf = JDOHelper.getPersistenceManagerFactory("Products");
		 
		
	}

	@Override
	public Cancion recuperarCancion(String nom) {
		Cancion cancion1 = null;
		pm = pmf.getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		tx.begin();
		try {
			Query query = pm.newQuery("SELECT FROM " + Cancion.class.getName()
					+ " WHERE titulo == '" + nom + "'");
		
			cancion1 = (Cancion) query.execute();

			tx.commit();
		} finally {
			if (tx.isActive() && tx != null) {
				tx.rollback();
			}

			pm.close();
		}

		return cancion1;
	}

	public List<cancionDTO> getListaCanciones() {
		PersistenceManager pm = pmf.getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		List<cancionDTO> canciones = new ArrayList<>();

		try {
			System.out.println("   * Recuperando Lista canciones.");

			tx.begin();
			Extent<Cancion> extent = pm.getExtent(Cancion.class, true);

			for (Cancion product : extent) {
				
				cancionAssembler.getInstance();
				canciones.add(cancionAssembler.createCancionDTO(product));
			}

			tx.commit();
		} catch (Exception ex) {
			System.out.println("   $ Error retreiving an extent: "
					+ ex.getMessage());
		} finally {
			if (tx != null && tx.isActive()) {
				tx.rollback();
			}

			pm.close();
		}
		

		return canciones;
	}

	@Override
	public void insertarCancion(Cancion c1) {
		this.insertarObjeto(c1);

	}

	@Override
	public void insertarUsuario(Usuario u1) {
		this.insertarObjeto(u1);

	}

	@Override
	public void insertarReproduccion(Reproduccion rep1) {
		this.insertarObjeto(rep1);

	}

	@Override
	public Usuario recuperarUsuario(String nombre) {
		pm = pmf.getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		Usuario usuario = null;

		try {
			System.out.println("   * Recuperadno el usuario: " + nombre);

			tx.begin();
			Query query = pm.newQuery("SELECT FROM " + Usuario.class.getName()
					+ " WHERE nombre == '" + nombre + "'");
			query.setUnique(true);
			usuario = (Usuario) query.execute();
			tx.commit();
		} catch (Exception ex) {
			System.out.println("   $ Error retreiving an extent: "
					+ ex.getMessage());
		} finally {
			if (tx != null && tx.isActive()) {
				tx.rollback();
			}

			pm.close();
		}
		return usuario;
	}

	@Override
	public Reproduccion recuperarReproduccion(int id) {
		pm = pmf.getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		Reproduccion rep = null;

		try {
			System.out.println("   * Recuperando reproduccion: " + id);

			tx.begin();
			Query query = pm.newQuery("SELECT FROM "
					+ Reproduccion.class.getName() + " WHERE id == " + id);
			query.setUnique(true);
			rep = (Reproduccion) query.execute();
			tx.commit();
		} catch (Exception ex) {
			System.out.println("   $ Error retreiving an extent: "
					+ ex.getMessage());
		} finally {
			if (tx != null && tx.isActive()) {
				tx.rollback();
			}

			pm.close();
		}
		return rep;
	}

	@Override
	public void insertarObjeto(Object obj) {
		pm = pmf.getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		tx.begin();
		try {
			pm.makePersistent(obj);
			tx.commit();
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}

			pm.close();
		}

	}

	@Override
	public void updateUsuario(Usuario u1) {
		PersistenceManager pm = pmf.getPersistenceManager();
	    Transaction tx = pm.currentTransaction();
	    
	    try {
	    	tx.begin();
	    	pm.makePersistent(u1);
	    	tx.commit();
	     } catch (Exception ex) {
		   	System.out.println("   $ Error retreiving an extent: " + ex.getMessage());
	     } finally {
		   	if (tx != null && tx.isActive()) {
		   		tx.rollback();
		   	}
				
	   		pm.close();
	     }
		
	}

}
